<?php
namespace Home\Controller;

use Think\Controller;

/**
 * 内容管理
 * @author DONGGUIYU
 *
 */
class ContentController extends Controller
{
    /**
     * 内容
     */
    public function contentList(){
        // 关键字
        $keyWord = I('post.keyword', "", 'htmlspecialchars'); 
        if(!empty($keyWord)){
            $where['detail'] = array('like', "%$keyWord%");
            $this->assign("keyWord", $keyWord);
        }
        // 内容MODEL
        $contentModel = D('Content');
        // 分页
        $page = setPage($contentModel, $where);
        // 进行分页数据查询
        $orderby['id']='asc';
        // 入口城市
        if(isset($where)){
            $contentList = $contentModel->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        } else {
            $contentList = $contentModel->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        }
        $show = $page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("contentList", $contentList);
        $this->display('content');
    }
    
    /**
     * 保存内容
     */
    public function saveContent(){
        // 内容ID
        $id =  I('post.id', "", 'htmlspecialchars');
        // 内容
        $detail = I('post.detail',"", 'htmlspecialchars');
        // 内容model
        $contentModel = D("Content");
        $data['detail'] = $detail;
    
        $contentModel->startTrans();
        if(empty($id)){
            // 添加内容
            $data['create_time'] = date('Y-m-d');
            $rlt = $contentModel->data($data)->add();
        } else {
            // 修改内容
            $where['id'] = $id;
            $rlt = $contentModel->where($where)->save($data);
        }
        if($rlt != 0 && $rlt == false){
            $contentModel->rollback();
            $this->error("保存失败！");
            exit;
        } else {
            $contentModel->commit();
            $this->success("保存成功！", "contentList");
            exit;
        }
    }
    
    /**
     * 删除内容
     */
    public function deleteContent(){
       $idList = I('post.idList', "", 'htmlspecialchars');
        // 内容model
        $contentModel = D('Content');
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $contentModel->startTrans();
            foreach ($idArray as $id){
                $where['id'] = $id;
                $rlt = $contentModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $contentModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
            }
            // 提交事务
            $contentModel->commit();
            $this->success("删除成功！", 'contentList');
        }
    }
    
    /**
     * 广告
     */
    public function advertisementList(){
        // 关键字
        $keyWord = I('post.keyword', "", 'htmlspecialchars');
        if(!empty($keyWord)){
            $where['title'] = array('like', "%$keyWord%");
            $this->assign("keyWord", $keyWord);
        }
        // 广告MODEL
        $advertisementModel = D('Advertisement');
        // 分页
        $page = setPage($advertisementModel, $where);
        // 进行分页数据查询
        $orderby['id']='asc';
        if(isset($where)){
            $advertisementList = $advertisementModel->where($where)->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        } else {
            $advertisementList = $advertisementModel->order($orderby)->limit($page->firstRow.','.$page->listRows)->select();
        }
        $show = $page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign("advertisementList", $advertisementList);
        $this->display('advertisement');
    }
    
    /**
     * 添加广告
     */
    public function addAdvertisement(){
        $this->assign('title','添加广告');
        $this->display('editadvertisement');
    }
    
    /**
     * 编辑广告
     */
    public function editAdvertisement(){
        $this->assign('title','修改广告');
        // 广告ID
        $id = I('get.id', "", 'htmlspecialchars');
        // 广告model
        $advertisementModel = D("advertisement");
        $where['id'] = $id;
        $rlt = $advertisementModel->where($where)->order("update_time desc")->select();
        if(isset($rlt)){
            $this-> assign('advertisement', $rlt[0]);
        }
        $this->display('editadvertisement');
    }
    
    /**
     * 保存广告
     */
    public function saveAdvertisement(){
        // 广告ID
        $id =  I('post.advertisementId', "", 'htmlspecialchars');
        // 广告标题
        $title = I('post.title',"", 'htmlspecialchars');
        // 链接地址
        $link_address = I('post.link_address',"", 'htmlspecialchars');
        // 广告model
        $advertisementModel = D("Advertisement");
        $data['title'] = $title;
        $data['link_address'] = $link_address;
    
        $advertisementModel->startTrans();
        if(empty($id)){
            // 添加内容
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['update_time'] = $data['create_time'];
            $rlt = $advertisementModel->data($data)->add();
        } else {
            // 修改内容
            $where['id'] = $id;
            $data['update_time'] = date('Y-m-d H:i:s');
            $rlt = $advertisementModel->where($where)->save($data);
        }
        $rootPath = './Public/Uploads/';
        if( $rlt == false){
            $advertisementModel->rollback();
            $this->error("保存失败！");
            exit;
        } else {
            if (empty($id)){
                $dirName =  base64_encode("advertisement_".$rlt);
            } else {
                $dirName = base64_encode("advertisement_".$id);
            }
            if(file_exists($rootPath.$dirName)){
                if(!is_dir($rootPath.$dirName)){
                    mkdir($rootPath.$dirName);
                }
            } else{
                mkdir($rootPath.$dirName);
            }
            // 文件上传
            $config = array(
                'maxSize'    =>    3145728,
                'rootPath'   =>    $rootPath,
                'savePath'   =>    '',
                'saveName'   =>    'uniqid',
                'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
                'autoSub'    =>    true,
                'subName'    =>    $dirName,
                'replace'    =>    true,
            );
            $upload = new \Think\Upload($config);// 实例化上传类
            
            if(!empty($_FILES["file"]["name"][0])){
                // 上传文件
                $info   =   $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $advertisementModel->rollback();
                    rmdir($rootPath.$dirName);
                    $this->error($upload->getError());
                }else{
                    // 上传成功 获取上传文件信息
                    $data['img_path'] =$info["file"]['savepath'].$info["file"]['savename'];
                }
            }
            if(empty($id)){
                // 添加景点
                $where['id'] = $rlt;
            }
            $rs = $advertisementModel->where($where)->save($data);
            if($rlt != 0 && $rlt == false){
                $advertisementModel->rollback();
                $this->error("保存失败！");
                exit;
            } else {
                $advertisementModel->commit();
                $this->success("保存成功！", "advertisementList");
                exit;
            }
        }
    }
    
    /**
     * 删除广告
     */
    public function deleteAdvertisement(){
        $idList = I('post.idList', "", 'htmlspecialchars');
        // 广告model
        $advertisementModel = D('Advertisement');
        if(!empty($idList)){
            $idArray = split(',', $idList);
            // 开启事务
            $advertisementModel->startTrans();
            foreach ($idArray as $id){
                $where['id'] = $id;
                $rlt = $advertisementModel->where($where)->delete();
                if($rlt == false){
                    // 删除失败时回滚事务
                    $advertisementModel->rollback();
                    $this->error("删除失败！");
                    exit;
                }
            }
            // 提交事务
            $advertisementModel->commit();
            $this->success("删除成功！", 'advertisementList');
        }
    }
}

?>